package com.jusda.message.api;

import com.jusda.message.entity.UserScore;
import com.jusda.message.producer.strategy.ProducerStrategyContext;
import com.jusda.message.producer.strategy.SendMode;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:y022 22/04/2022 23:28
 * @description:
 */
@RequestMapping("msg")
@RestController
public class MessageApi {

    /**
     * 提供手动启动调度任务的接口
     *
     * @param userScoreList 消息列表
     * @throws MQClientException
     * @throws InterruptedException
     */
    @PostMapping("batch-send-msg")
    public void batchSendMsg(@RequestBody List<UserScore> userScoreList) throws MQClientException {
        ProducerStrategyContext.selectSendMode(SendMode.ASYNC).batchSendMessage(userScoreList);
    }

    /**
     * 对外提供单条消息发送接口
     *
     * @param userScore 消息本身
     * @throws MQClientException 可能抛出的异常
     */
    @PostMapping("send-msg/{s}")
    public void sendMag(@PathVariable("s") SendMode sendMode, @RequestBody UserScore userScore
                        ) throws MQClientException {
        ProducerStrategyContext.selectSendMode(sendMode).sendMessage(userScore);
    }

}
